package com.taobao.taokeeper.message.impl;

import static com.taobao.common.toolkit.constant.SymbolConstant.COMMA;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taobao.common.toolkit.util.ListUtil;
import com.taobao.common.toolkit.util.StringUtil;
import com.taobao.jm.msgcenter.MsgManager;
import com.taobao.jm.msgcenter.common.MsgConstants;
import com.taobao.jm.msgcenter.common.Result;
import com.taobao.taokeeper.common.SystemInfo;
import com.taobao.taokeeper.common.constant.SystemConstant;
import com.taobao.taokeeper.message.MessageSender;
import com.taobao.taokeeper.model.type.EnvType;
import com.taobao.taokeeper.model.type.Message;

/**
 * Description: 淘宝内部使用：旺旺消息与手机短信
 * @author 银时 yinshi.nc@taobao.com
 * @Date Dec 26, 2011
 */
public class TbMessageSender implements MessageSender {

	private static final Logger LOG = LoggerFactory.getLogger( TbMessageSender.class );

	private Message message;
	

	public TbMessageSender( Message message ) {
		this.message = message;
	}

	@Override
	public void run() {
		try {
			
			this.sendMessage( StringUtil.trimToEmpty( message.getTargetAddresses() ), 
					                 StringUtil.trimToEmpty( message.getSubject() ), 
					                 StringUtil.trimToEmpty( message.getContent() ),
					                 StringUtil.trimToEmpty( message.getType().toString() ) 
					                 );
		} catch ( Exception e ) {
			e.printStackTrace();
			LOG.error( "发送消息失败：" + message + e.getMessage() );
		}
	}
	
	
	/**
	 * 发送消息
	 * @param targetAddresses	目标地址
	 * @param subject			主题
	 * @param content			内容
	 * @param channel			消息类型
	 * @return
	 */
	private Result sendMessage( String targetAddresses, String subject, String content, String channel ) {

		if ( StringUtil.isBlank( targetAddresses ) || StringUtil.isBlank( channel ) )
			return null;
		Result result = null;
		String sendType = MsgConstants.CUSTOM_SERVER_LIST_TYPE;
		try {

			MsgManager mcm = new MsgManager();

			mcm.setServiceType( MsgConstants.CUSTOM_SERVER_LIST_TYPE );
			mcm.setCustomServerHosts( SystemConstant.serverOfMsgCenter );
			mcm.init();

			if ( EnvType.DAILY.toString().equals( SystemInfo.envName ) ) {
				mcm.setServiceType( MsgConstants.CUSTOM_SERVER_LIST_TYPE );
				mcm.setCustomServerHosts( SystemConstant.serverOfMsgCenter );
				mcm.init();
			} else if ( EnvType.ONLINE.toString().equals( SystemInfo.envName ) || EnvType.PREPARE.toString().equals( SystemInfo.envName ) ) {
				sendType = "HSF";
				mcm.setServiceGroup( "HSF" );
				mcm.setServiceVersion( "1.0.0" );
				mcm.init();
			}

			String sourceId      = SystemConstant.sourceIdOfMsgCenter;
			String templateId    = SystemConstant.templateIdOfMsgCenter;
			String messageTypeId = SystemConstant.messageTypeIdOfMsgCenter;
			
			List< String > targetAddressList = ListUtil.parseList( StringUtil.trimToEmpty( targetAddresses ), COMMA );
			for ( String targetAddress : targetAddressList ) {
				result = mcm.sendMsg( targetAddress, "title: " + subject, "content: " + content, channel, sourceId, templateId, messageTypeId );
			}

			if ( result.isSuccess() ) {
				LOG.warn( "Send " + channel + " message success, send type is " + sendType + ", targetAddress: " + targetAddresses + ", subject: "
						+ subject + ", content: " + content );
			} else {
				LOG.warn( "Send " + channel + " message failure, send type is " + sendType + ", targetAddress: " + targetAddresses + ", subject: "
						+ subject + ", content: " + content );
			}

		} catch ( Exception e ) {
			LOG.warn( "Send " + channel + " message failure, send type is " + sendType + ", targetAddress: " + targetAddresses + ", subject: "
					+ subject + ", content: " + content );
			LOG.error( e.getMessage() );
		}
		return result;
	}
	
}
